package com.property.manage.controller.rent;

import com.guardchina.framework.mybatis.model.CustomPage;
import com.property.manage.model.owner.OwnerEntity;
import com.property.manage.model.print.ArrearsPayBillVo;
import com.property.manage.model.print.PayBillVo;
import com.property.manage.model.rent.HouseArrearsVo;
import com.property.manage.model.rent.RentPaymentVo;
import com.property.manage.model.rent.RentVo;
import com.property.manage.service.rent.HouseArrearsService;
import com.property.manage.service.rent.HouseRentService;
import com.property.manage.service.rent.RentPaymentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Api(tags = "房屋租金")
@RestController
@RequestMapping("/rentPayment")
@AllArgsConstructor
@Validated
public class RentPaymentController {
    @Autowired
    private RentPaymentService rentPaymentService;
    @Autowired
    private HouseArrearsService houseArrearsService;


    /*@ApiOperation("房屋租金欠费清缴")
    @PostMapping("/add")
    public void add(@ApiParam("小区id") @RequestParam(value = "villageId") @NotNull(message = "小区id不能为空") Integer villageId,
                    @ApiParam("租金") @RequestParam(value = "money") @NotNull(message = "租金不能为空") BigDecimal money,
                    @ApiParam("现金") @RequestParam(value = "cash") @NotNull(message = "现金不能为空") BigDecimal cash,
                    @ApiParam("使用预存金额") @RequestParam(value = "preDeposit", required = false) BigDecimal preDeposit,
                    @ApiParam("零钱转存金额") @RequestParam(value = "small") @NotNull(message = "零钱不能为空") BigDecimal small,
                    @ApiParam("总金额") @RequestParam(value = "total") @NotNull(message = "总金额不能为空") BigDecimal total,
                    @ApiParam("物业费") @RequestParam(value = "propertyFee") @NotNull(message = "物业费不能为空") BigDecimal propertyFee,
                    @ApiParam("其他费用") @RequestParam(value = "otherFee") @NotNull(message = "其他费用不能为空") BigDecimal otherFee,
                    @ApiParam("欠费记录id") @RequestParam(value = "ids") @NotEmpty(message = "欠费id不能为空") List<Integer> ids,
                    @ApiParam("付款方式") @RequestParam(value = "type") @NotNull(message = "付款方式不能为空") int type,
                    @ApiParam("打印票据传1") @RequestParam(value = "print", required = false) Integer print,
                    @ApiParam("零钱转存传1") @RequestParam(value = "isSmall", required = false) Integer isSmall,
                    @ApiParam("使用预存传1") @RequestParam(value = "pre", required = false) Integer pre,
                    @ApiParam("楼栋") @RequestParam(value = "building") @NotNull(message = "楼栋id不能为空") Integer building,

                    @ApiIgnore @RequestParam("userId") @NotNull(message = "您尚未登录") Integer userId
    ) {
        rentPaymentService.add2(money, type, preDeposit, ids, print, small, isSmall, pre, total, cash, building, villageId, propertyFee, otherFee, userId);
    }*/

    @ApiOperation("房屋租金欠费清缴")
    @PostMapping("/add")
    public ArrearsPayBillVo charge(@ApiIgnore("用户编号") @RequestParam("userId")  @NotNull(message = "您尚未登录") Integer userId,
                                   @ApiParam("楼栋编号") @RequestParam("buildingId") @NotNull(message = "楼栋编号为空") Integer buildingId,
                                   @ApiParam("欠费编号（转换成json数据）") @RequestParam("arrearsIds") @NotBlank(message = "欠费编号为空") String arrearsIds,
                                   @ApiParam("合计金额") @RequestParam("totalMoney") @NotNull(message = "合计金额为空") BigDecimal totalMoney,
                                   @ApiParam("收款金额") @RequestParam("chargeMoney") @NotNull(message = "收款金额") BigDecimal chargeMoney,
                                   @ApiParam("支付方式") @RequestParam("payType") @NotNull(message = "支付方式") Integer payType,
                                   @ApiParam("使用预存") @RequestParam(value = "useStorage", required = false, defaultValue = "false") Boolean useStorage,
                                   @ApiParam("零钱转存") @RequestParam(value = "storageReturn", required = false, defaultValue = "false")  Boolean storageReturn,
                                   @ApiParam("是否打印") @RequestParam(value = "print", required = false, defaultValue = "false")  Boolean print,
                                   @ApiParam("备注") @RequestParam(value = "remark", required = false)  String remark){

        return houseArrearsService.charge(userId, buildingId, arrearsIds, totalMoney, chargeMoney, payType, useStorage, storageReturn, print, remark);
    }


    @ApiOperation("租金列表")
    @GetMapping("/page")
    public CustomPage<RentPaymentVo> getPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                             @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                             @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                             @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                             @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                             @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                             @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                             @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime) {
        return rentPaymentService.page(pageNo, pageSize, villageId, building, unit, roomNumber, startTime, endTime);
    }


    @ApiOperation("缴费删除")
    @PostMapping("/pay/delete")
    public void delete(@ApiParam("缴费编号") @RequestParam("id") @NotNull(message = "缴费编号为空") Integer id){

        rentPaymentService.deletePay(id);
    }

    @ApiOperation("房屋租金欠费列表")
    @GetMapping("/arrearsPage")
    public CustomPage<HouseArrearsVo> arrearsPage(@ApiParam("当前页码") @RequestParam("pageNo") @NotNull(message = "请输入当前页码") Long pageNo,
                                                  @ApiParam("每页条数") @RequestParam("pageSize") @NotNull(message = "请输入每页条数") Long pageSize,
                                                  @ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                                                  @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                                                  @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                                                  @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                                                  @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                                                  @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime) {
        return houseArrearsService.page(pageNo, pageSize, villageId, building, unit, roomNumber, startTime, endTime);
    }

    @ApiOperation("欠费清缴页面")
    @GetMapping("/getByBuildingId")
    public HouseArrearsVo getByBuildingId(
                                             @ApiParam("楼栋") @RequestParam(value = "buildingId") @NotNull(message = "楼栋编号为空") Integer buildingId
                                             ) {
        return houseArrearsService.getByBuildingId(buildingId);
    }

    @GetMapping("/rentPay")
    @ResponseBody
    @ApiOperation(value = "房屋租金缴费记录导出接口")
    public void rentPay(@ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                        @ApiParam("楼栋") @RequestParam(value = "building", required = false) Integer building,
                        @ApiParam("单元") @RequestParam(value = "unit", required = false) Integer unit,
                        @ApiParam("房号") @RequestParam(value = "roomNumber", required = false) String roomNumber,
                        @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                        @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                        HttpServletRequest request, HttpServletResponse response) {
        rentPaymentService.download(villageId,building, unit, roomNumber, startTime,endTime,request,response);
    }

    @GetMapping("/arrears")
    @ResponseBody
    @ApiOperation(value = "房屋租金欠费导出接口")
    public void arrears(@ApiParam("小区id") @RequestParam(value = "villageId", required = false) Integer villageId,
                        @ApiParam("开始时间") @RequestParam(value = "startTime", required = false) String startTime,
                        @ApiParam("结束时间") @RequestParam(value = "endTime", required = false) String endTime,
                        HttpServletRequest request, HttpServletResponse response) {
        houseArrearsService.download(villageId,startTime,endTime,request,response);
    }

    @ApiOperation("打印票据")
    @GetMapping("/pay/bill")
    public PayBillVo payBill(@ApiParam("支付编号") @RequestParam(value = "id")@NotNull(message = "支付编号为空") Integer id){

        return rentPaymentService.payBill(id);
    }
}
